<?php

require_once '../include/common.inc.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

//ɲ룬£ɾȲˢ
if($continue!="")header("localtion: c_promoter_commission.php"); 

$menu_index = 2;
$page_title = 'TABLA';
$guide_message = 'Customer | Promoter Commission';
$page_url = 'c_promoter_commission.php?';
$ppp = 20;

getMyParam('action');
getMyParam('search_key');

if($action!=""){
    getMyParam('status');
	getMyParam('pay_date');				
	getMyParam('cid');	
		
if ($action =="edit") {
		$pay_date=trim($pay_date);
		$sql="update `promoter_commission` SET `pay_date` = '".strtotime($pay_date)."',
			`status` = '".$status."' where id=".$cid;

        $db->query($sql);
		$msg="Successfully updated promoter_commission info.";
		$href="c_promoter_commission.php";
		include_once('menu_list.php');
		require_once PrintEot('a_header');
		require_once PrintEot('a_msg');
		require_once PrintEot('a_footer');
		exit;

} 
if ($action == "del") {
    getMyParam('cid');
    if (is_numeric($cid)) {
        $sql = "DELETE FROM `promoter_commission` WHERE `id` = '" . $cid . "'";
        $db->query($sql);
		$msg="This promoter commission has been deleted. ";
		$href="c_promoter_commission.php";
		include_once('menu_list.php');
		require_once PrintEot('a_header');
		require_once PrintEot('a_msg');
		require_once PrintEot('a_footer');
		exit;
    }
}

}

//$sql_where = '';-------------leo
//if ($action == "search" && trim($search_key)!= "") {
//    $sql_where = "where `id`='".$search_key."'  OR `promoter_id`='".$search_key."'  OR `order_id`='".$search_key."' ";
//}
$sql_where = " where o.deliver_status<>2";////select commission which deliver_status is no canceled(2)
$sql_leftjoin=" left join cus_order o on cm.order_id=o.id";
if ($action == "search" && trim($search_key)!= "") {
    $sql_where = $sql_where." and (cm.id='".$search_key."'  OR cm.promoter_id='".$search_key."'  OR cm.`order_id`='".$search_key."')";
}



//$sql = "SELECT `id` FROM `promoter_commission` " . $sql_where;-------------leo
$sql = "SELECT cm.id FROM `promoter_commission` cm" .$sql_leftjoin. $sql_where;
$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);


// *** page code start ***
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}
// *** page code end ***


//$sql = "SELECT * FROM  promoter_commission  ".$sql_where. "ORDER BY `id` DESC LIMIT $start_limit, $ppp";--leo
$sql = "SELECT * FROM  promoter_commission  cm ".$sql_leftjoin.$sql_where. " ORDER BY cm.id DESC LIMIT $start_limit, $ppp";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    if ($action == "search" && trim($search_key) != "") {
        $rs['id'] = str_replace($search_key, "<font color=red>$search_key</font>", $rs['id']);
        $rs['promoter_id'] = str_replace($search_key, "<font color=red>$search_key</font>", $rs['promoter_id']);
		$rs['order_id'] = str_replace($search_key, "<font color=red>$search_key</font>", $rs['order_id']);
    }
    $out_ary[] = $rs;

}

for($i=0;$i<count($out_ary);$i++)
{
	$out_ary[$i]['commission']=number_format($out_ary[$i]['commission'],2);
	$out_ary[$i]['date']=date('Y-m-d',$out_ary[$i]['date']);
	
	if($out_ary[$i]['status']==0){
	$out_ary[$i]['status']="No";
	$out_ary[$i]['pay_date']="";
	}
	if($out_ary[$i]['status']==1){
	$out_ary[$i]['status']="Yes";
	$out_ary[$i]['pay_date']=date('Y-m-d',$out_ary[$i]['pay_date']);
	}
	
	//GET promoter name
	 $sql = "SELECT * FROM `promoter_info` where id='" . $out_ary[$i]['promoter_id']."'";
	 
	 $query = $db->query($sql);
	 $rs = $db->fetch_array($query);
	 $out_ary[$i]['promoter_name']=$rs['firstname'].$rs['lastname'];
}


$page_show = getPageStr($sql_all_num, $page, $ppp, $page_url);

include_once('menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_c_promoter_commission');
require_once PrintEot('a_footer');

?>